import { v4 as uuid } from 'uuid'
import { parseFullContent, inTermBlock } from '@/utils/helpers'
import { isListTitle } from '@/constants/structure'

export class StdTextDataV2 {
  constructor({ dataId, dataLang = 'zh', contentList, isExample, bizExtra }) {
    // 数据唯一标识
    this.dataId = dataId || uuid()
    // 中英文区分 zh:中文，en:英文
    this.dataLang = dataLang
    // 是否为示例内容
    this.isExample = isExample
    // 变量数组
    this.contentList = contentList?.map?.((i) => new StdContentV2(i)) || []
    if (bizExtra) this.bizExtra = bizExtra
  }
}

class StdContentV2 {
  constructor({
    contentType = 'text',
    linkUrl,
    contentNumber,
    content,
    fullContent,
    dataId,
    bizExtra,
    formulaData,
    contentEn,
    fullContentEn,
    figureData
  }) {
    // 具体内容类型：text（默认文本), link(超链接), sup(上标), sub（下标）,footnote（脚注）, department(组织结构)
    // refdoc引用标准编号或文件, refchapter引用标准内容, keyword关键词, bizIndex索引
    this.contentType = contentType
    // 编号：场景有附录编号、参考文献编号、脚注编号等
    if (contentNumber) this.contentNumber = contentNumber
    // 不包含编号的具体内容
    this.content = content
    // 包含编号的完整数据 - 后端只关注这个属性
    this.fullContent = fullContent
    // 脚注的dataId
    if (dataId) this.dataId = dataId
    if (bizExtra) this.bizExtra = bizExtra
    if (contentEn) this.contentEn = contentEn
    if (linkUrl) this.linkUrl = linkUrl
    if (fullContentEn) this.fullContentEn = fullContentEn
    // 行内公式
    if (formulaData) this.formulaData = formulaData
    // 行内图片
    if (figureData) this.figureData = figureData
  }
}

export class StdTextsV2 {
  constructor(node) {
    this.node = node
    this.contentList = []
  }

  get data() {
    if (isListTitle(this.node?.dataset?.type) && !inTermBlock(this.node)) {
      const fullContent = this.node?.innerText
      const { content, contentEn } = parseFullContent(fullContent)
      return {
        dataId: this.node?.dataset?.id,
        dataLang: this.node?.dataset?.lang,
        contentList: [{ contentType: 'text', content, contentEn, fullContent }]
      }
    }

    this.getContent(this.node)
    return {
      dataId: this.node?.dataset?.id,
      dataLang: this.node?.dataset?.lang || 'zh',
      contentList: this.contentList
    }
  }

  /** 具体的文本点内容，作为行内元素 */
  getContent(node) {
    if (!node) return
    if (node.nodeType === 8) {
      // 过滤掉注释
      if (node.nextSibling) {
        this.getContent(node.nextSibling)
      }
      return
    }
    const attrMap = node.dataset
    console.log("------------dataset-------------")
    console.log(attrMap)
    const dataType = attrMap?.type
    let contentData = {}
    if (dataType === 'footnote') {
      // 脚注
      contentData = {
        contentType: dataType,
        contentNumber: attrMap?.number,
        content: node.textContent,
        dataId: attrMap?.id,
        fullContent: `${attrMap?.number})`
      }
    } else if (dataType === 'inline_figure') {
      contentData = {
        contentType: dataType,
        dataId: attrMap.id,
        figureData: {
          attributes: {
            url: node?.src,
            width: node?.width,
            height: node?.height
          }
        }
      }
    } else if (dataType === 'inline_formula') {
      contentData = {
        contentType: dataType,
        dataId: attrMap.id,
        formulaData: {
          url: node?.src,
          width: node?.width,
          height: node?.height,
          latex: attrMap?.latex || ''
        }
      }
    } else if (['sub', 'sup', 'department', 'keyword', 'bizIndex'].includes(dataType)) {
      contentData = {
        contentType: dataType,
        contentNumber: null,
        content: node.textContent,
        fullContent: node.textContent
      }
    } else if (dataType === 'refdoc' || dataType === 'refchapter') {
      contentData = {
        contentType: dataType,
        contentNumber: null,
        linkUrl: attrMap?.linkUrl,
        bizExtra: JSON.stringify({
          refname: attrMap?.refname,
          standardId: attrMap?.standardid,
          mainContentId: attrMap?.maincontentid,
          refclassification: attrMap?.refclassification,
          refcode: attrMap?.refcode || attrMap?.refcontent || '',
          refdate: attrMap?.refdate,
          refanchor: attrMap?.refanchor,
          docno: attrMap?.docno || '',
          refversion: attrMap?.refversion
        }),
        content: attrMap?.refcontent,
        fullContent: node.innerText,
        dataId: attrMap?.id
      }
    } else {
      const textContent = node.textContent.replace(/[\u200B-\u200D\uFEFF]/g, '')
      // 纯文本节点
      contentData = {
        contentType: 'text',
        contentNumber: null,
        content: textContent,
        fullContent: textContent
      }
    }
    this.contentList.push(contentData)
    if (node.nextSibling) {
      this.getContent(node.nextSibling)
    }
  }
}
